package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.CourseTeacherDto;
import com.xuecheng.content.model.dto.EditCourseTeacherDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class CourserTeacherServiceImpl implements CourseTeacherService {

    private final CourseTeacherMapper courseTeacherMapper;

    private final CourseBaseMapper courseBaseMapper;

    /**
     * 根据课程id查询课程教师信息
     *
     * @param courseId 课程id
     * @return
     */
    @Override
    public List<CourseTeacherDto> listCourseTeacher(Long courseId) {
        // 1.先根据课程id查询课程教师信息
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(
                new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getCourseId, courseId));

        // 2.将courseTeacher集合转换为AddCourseTeacherDto集合
        return courseTeachers.stream().map(item -> {
            CourseTeacherDto courseTeacherDto = new CourseTeacherDto();
            BeanUtils.copyProperties(item, courseTeacherDto);
            return courseTeacherDto;
        }).collect(Collectors.toList());
    }

    /**
     * 新增或者修改教师信息
     *
     * @param companyId            机构id
     * @param editCourseTeacherDto 教师信息
     * @return
     */
    @Override
    public CourseTeacherDto insertOrUpdate(EditCourseTeacherDto editCourseTeacherDto, Long companyId) {
        // 1.将dto转为po
        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(editCourseTeacherDto, courseTeacher);

        // 2.校验是否为本机构的老师
        checkCompanyId(courseTeacher.getCourseId(), companyId);

        // 3.先获取到id 判断是新增操作 还是 修改操作
        Long id = editCourseTeacherDto.getId();
        if (id == null) {
            // 说明是新增操作
            courseTeacher.setCreateDate(LocalDateTime.now());
            int insert = courseTeacherMapper.insert(courseTeacher);
            if (insert <= 0) {
                XueChengPlusException.cast("添加课程教师信息失败");
            }
        } else {
            // 修改操作
            int update = courseTeacherMapper.updateById(courseTeacher);
            if (update <= 0) {
                XueChengPlusException.cast("修改课程教师信息失败");
            }
        }

        // 4.去查询教师信息返回
        return getCourseTeacherDto(courseTeacher.getId());
    }

    /**
     * 删除教师信息
     *
     * @param courseId 课程id
     * @param id       教师id
     */
    @Override
    public void deleteCourseTeacher(Long courseId, Long id, Long companyId) {
        // 1.校验是否为本机构的老师
        checkCompanyId(courseId, companyId);
        // 2.删除教师信息
        int delete = courseTeacherMapper.deleteById(id);
        if (delete <= 0) {
            XueChengPlusException.cast("删除课程教师信息失败");
        }
    }


    /**
     * 校验是否为本机构的老师
     *
     * @param courseId  课程id
     * @param companyId 机构id
     */
    private void checkCompanyId(Long courseId, Long companyId) {
        // 1.根据课程id查询出课程对象
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        // 2.判断是否为本机构和的老师
        if (!companyId.equals(courseBase.getCompanyId())) {
            XueChengPlusException.cast("本机构只能修改本机构的课程");
        }
    }

    /**
     * 根据id查询教师信息
     *
     * @param courseTeacherId 教师id
     * @return 返回教师信息
     */
    private CourseTeacherDto getCourseTeacherDto(Long courseTeacherId) {
        // 1.根据教师id查询教师信息
        CourseTeacher courseTeacher = courseTeacherMapper.selectById(courseTeacherId);
        // 2.转po为dto
        CourseTeacherDto courseTeacherDto = new CourseTeacherDto();
        BeanUtils.copyProperties(courseTeacher, courseTeacherDto);
        // 3.返回
        return courseTeacherDto;
    }
}
